package com.onlineauction.servlets;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.Rating;
import com.google.appengine.datanucleus.Utils;
import com.onlineauction.entities.Review;
import com.onlineauction.helpers.Constants;
import com.onlineauction.helpers.Utili;

public class ReviewServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8875275626421582579L;
	private HttpServletResponse response;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		response = resp;
		// TODO Auto-generated method stub
		HttpSession session = req.getSession(true);
		String username = (String) session.getAttribute(Constants.USERNAME);
		
		// user not logged in
		if (username == null) 
		{
			resp.sendRedirect("/LoginAccount");
			return;
		}
		
		String revieweeName = req.getParameter(Constants.REVIEWEE);
		if(revieweeName == null || revieweeName.isEmpty()){
			resp.getWriter().println("Please specify who you want to review [parameter reviewee]");
			return;
		}
		
		List<Review> reviews = Review.getReviewsForUser(revieweeName);
		
		req.setAttribute(Constants.REVIEWEE, revieweeName);
		req.setAttribute("reviews", reviews);
		
		req.getRequestDispatcher("/review.jsp").forward(req, resp);
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		response = resp;
		// TODO Auto-generated method stub
		HttpSession session = req.getSession(true);
		String username = (String) session.getAttribute(Constants.USERNAME);
		// user not logged in
		if (username == null) 
		{
			resp.sendRedirect("/LoginAccount");
			return;
		}
		boolean isValid=true;
		String message= "";
		//validate input
		String title = req.getParameter(Constants.TITLE);
		String reviewee = req.getParameter(Constants.REVIEWEE);
		String comment = req.getParameter(Constants.COMMENT);
		String ratingString = req.getParameter(Constants.RATING);
		
		if(!Utili.isValidString(ratingString)){	
			//
			isValid = false;
			message += "Rating is empty\n"; 
		}
		if(!Utili.isValidString(title)){
			isValid = false;
			message +="Title is empty\n";
		}
		if(!Utili.isValidString(comment)){
			isValid = false;
			message += "Comment is empty\n";
		}
		
		if(!isValid){
			handleException(message);
			return;
		}
		
		int rating = -1;
		try{
			rating = Integer.parseInt(ratingString);
		}catch(Exception e){
			handleException(message);
			return;
		}
		Date timestamp = new Date();
		Review.createReview(username, reviewee, rating, comment, timestamp, title, true);
		resp.sendRedirect("/review?"+Constants.REVIEWEE+"="+reviewee);

	}
	
	private void handleException(String message) throws ServletException, IOException{
		response.getWriter().print(message);
	}

}
